<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="rescueOrdersCode">
        <el-input
          v-model="queryParams.rescueOrdersCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单时间" prop="rescueOrdersTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.rescueOrdersTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择订单创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="救援时间" prop="rescueOrdersHelpTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.rescueOrdersHelpTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择救援时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="描述" prop="rescueOrdersDetails">
        <el-input
          v-model="queryParams.rescueOrdersDetails"
          placeholder="请输入描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="车牌号" prop="rescueOrdersVehicleNo">
        <el-input
          v-model="queryParams.rescueOrdersVehicleNo"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="用户标识—外键" prop="rescueOrdersPerId">
        <el-input
          v-model="queryParams.rescueOrdersPerId"
          placeholder="请输入用户标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="联系人姓名" prop="rescueOrdersPerName">
        <el-input
          v-model="queryParams.rescueOrdersPerName"
          placeholder="请输入联系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="手机号" prop="rescueOrdersPhone">
        <el-input
          v-model="queryParams.rescueOrdersPhone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="技师标识—外键" prop="rescueOrdersTechnicianId">
        <el-input
          v-model="queryParams.rescueOrdersTechnicianId"
          placeholder="请输入技师标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
    <!--  <el-form-item label="救援项目标识—外键" prop="rescueOrdersProjectId">
        <el-input
          v-model="queryParams.rescueOrdersProjectId"
          placeholder="请输入救援项目标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="订单类型" prop="rescueOrdersType">
          <el-select v-model="queryParams.rescueOrdersType" placeholder="请选择订单状态" clearable size="small">
            <el-option :value="0" label="未接单"></el-option>
            <el-option :value="1" label="已接单"></el-option>
            <el-option :value="2" label="处理中"></el-option>
            <el-option :value="3" label="已完成"></el-option>
            <el-option :value="4" label="已取消"></el-option>
          </el-select>
      </el-form-item>
     <!-- <el-form-item label="备注" prop="rescueOrdersContent">
        <el-input
          v-model="queryParams.rescueOrdersContent"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="经度" prop="rescueOrdersLongitude">
        <el-input
          v-model="queryParams.rescueOrdersLongitude"
          placeholder="请输入经度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
    <!--  <el-form-item label="纬度" prop="rescueOrdersLatitude">
        <el-input
          v-model="queryParams.rescueOrdersLatitude"
          placeholder="请输入纬度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    <!--  <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:orders:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:orders:edit']"
        >修改</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orders:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="rescueOrdersCode" width="100"/>
      <el-table-column label="订单创建时间" align="center" prop="rescueOrdersTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rescueOrdersTime, '{y}年{m}月{d} {h}时{m}分') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="救援时间" align="center" prop="rescueOrdersHelpTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rescueOrdersHelpTime, '{y}年{m}月{d} {h}时{m}分') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="rescueOrdersDetails"  width="200"/>
      <el-table-column label="车牌号" align="center" prop="rescueOrdersVehicleNo" width="100"/>
      <el-table-column label="用户" align="center" prop="rescueOrdersPerId">
			<template slot-scope="scope">
				{{scope.row.personage.isTechnician !=null ? (scope.row.personage.isTechnician == 0 ? scope.row.personage.username:'是技师'):'暂无'}}
			</template>
	  </el-table-column>
      <el-table-column label="联系人" align="center" prop="rescueOrdersPerName" />
      <el-table-column label="手机号" align="center" prop="rescueOrdersPhone" width="110"/>
      <el-table-column label="技师" align="center" prop="rescueOrdersTechnicianId">
		<template slot-scope="scope">
			{{scope.row.personage.isTechnician !=null ? (scope.row.personage.isTechnician == 1 ? scope.row.personage.username:'是用户'):'暂无'}}
		</template>
	   </el-table-column>
      <el-table-column label="救援项目" align="center" prop="rescueOrdersProjectId" width="120">
		  <template slot-scope="scope">
		  	{{scope.row.rescueProject.rescueProjectName}}
		  </template>
	  </el-table-column>
      <el-table-column label="订单状态" align="center" prop="rescueOrdersType">
		  <template slot-scope="scope">
		  		{{scope.row.rescueOrdersType == 0 ? '未接单':(scope.row.rescueOrdersType == 1 ? '已接单' : (scope.row.rescueOrdersType==2 ? '处理中':(scope.row.rescueOrdersType==3 ? '已完成':(scope.row.rescueOrdersType==4 ? '已取消':'null'))))}}
		  </template>
	  </el-table-column>
      <el-table-column label="备注" align="center" prop="rescueOrdersContent" width="180"/>
      <el-table-column label="地址" align="center" prop="rescueOrdersPath" width="150"/>
      <el-table-column label="经度" align="center" prop="rescueOrdersLongitude">
		  <template slot-scope="scope">
		  	{{scope.row.rescueOrdersLongitude + '°'}}
		  </template>
	  </el-table-column>
      <el-table-column label="纬度" align="center" prop="rescueOrdersLatitude">
		  <template slot-scope="scope">
		  	{{scope.row.rescueOrdersLatitude + '°'}}
		  </template>
	  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:orders:edit']"
          >修改</el-button>
		<el-dropdown trigger="click" size="mini">
					   <el-button
						 size="mini"
						 type="text"
						 class="el-dropdown-link">
					   更多操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
			  <el-dropdown-menu slot="dropdown">
				<el-dropdown-item>
						 <el-button
						   size="mini"
						   type="text"
						   icon="el-icon-circle-check"
						   @click="handleUpdatee(scope.row)"
						   v-hasPermi="['system:orders:add']"
						 >去发货</el-button>
						 </el-dropdown-item>
			  </el-dropdown-menu>
			</el-dropdown>
		<!-- 	<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:orders:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!-- 添加或修改订单对话框 -->
	<el-dialog :title="title" :visible.sync="openss" width="500px" append-to-body>
	  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
		  <el-form-item label="订单号" prop="rescueOrdersCode">
		     <el-input v-model="form.rescueOrdersCode" placeholder="请输入订单号" />
		   </el-form-item> <!-- 
	    <el-form-item label="订单状态">
	      <el-select v-model="form.ordersStatus" placeholder="请选择订单状态">
			  
			<el-option :value="2" label="已发货"></el-option>
	      </el-select>
	    </el-form-item> -->
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitFormm">确 定</el-button>
	    <el-button @click="cancell">取 消</el-button>
	  </div>
	</el-dialog>
	
    <!-- 添加或修改救援订单 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
       <!-- <el-form-item label="订单号" prop="rescueOrdersCode">
          <el-input v-model="form.rescueOrdersCode" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单时间" prop="rescueOrdersTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.rescueOrdersTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择订单创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="救援时间" prop="rescueOrdersHelpTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.rescueOrdersHelpTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择救援时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述" prop="rescueOrdersDetails">
          <el-input v-model="form.rescueOrdersDetails" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="车牌号" prop="rescueOrdersVehicleNo">
          <el-input v-model="form.rescueOrdersVehicleNo" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="用户" prop="rescueOrdersPerId">
          <el-input v-model="form.rescueOrdersPerId" placeholder="请输入用户标识—外键" />
        </el-form-item>
        <el-form-item label="联系人" prop="rescueOrdersPerName">
          <el-input v-model="form.rescueOrdersPerName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="rescueOrdersPhone">
          <el-input v-model="form.rescueOrdersPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="技师" prop="rescueOrdersTechnicianId">
          <el-input v-model="form.rescueOrdersTechnicianId" placeholder="请输入技师标识—外键" />
        </el-form-item>
        <el-form-item label="救援项目" prop="rescueOrdersProjectId">
          <el-input v-model="form.rescueOrdersProjectId" placeholder="请输入救援项目标识—外键" />
        </el-form-item> -->
        <el-form-item label="订单状态">
          <el-select v-model="form.rescueOrdersType" placeholder="请选择订单状态">
				<el-option :value="0" label="未接单"></el-option>
				<el-option :value="1" label="已接单"></el-option>
				<el-option :value="2" label="处理中"></el-option>
				<el-option :value="3" label="已完成"></el-option>
				<el-option :value="4" label="已取消"></el-option>
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="备注" prop="rescueOrdersContent">
          <el-input v-model="form.rescueOrdersContent" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="地址" prop="rescueOrdersPath">
          <el-input v-model="form.rescueOrdersPath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="经度" prop="rescueOrdersLongitude">
          <el-input v-model="form.rescueOrdersLongitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="rescueOrdersLatitude">
          <el-input v-model="form.rescueOrdersLatitude" placeholder="请输入纬度" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders, exportOrders } from "@/api/system/rescueOrders";

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 救援订单 表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
	  openss:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rescueOrdersCode: undefined,
        rescueOrdersTime: undefined,
        rescueOrdersHelpTime: undefined,
        rescueOrdersDetails: undefined,
        rescueOrdersVehicleNo: undefined,
        rescueOrdersPerId: undefined,
        rescueOrdersPerName: undefined,
        rescueOrdersPhone: undefined,
        rescueOrdersTechnicianId: undefined,
        rescueOrdersProjectId: undefined,
        rescueOrdersType: undefined,
        rescueOrdersContent: undefined,
        rescueOrdersPath: undefined,
        rescueOrdersLongitude: undefined,
        rescueOrdersLatitude: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询救援订单 列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rescueOrdersId: undefined,
        rescueOrdersCode: undefined,
        rescueOrdersTime: undefined,
        rescueOrdersHelpTime: undefined,
        rescueOrdersDetails: undefined,
        rescueOrdersVehicleNo: undefined,
        rescueOrdersPerId: undefined,
        rescueOrdersPerName: undefined,
        rescueOrdersPhone: undefined,
        rescueOrdersTechnicianId: undefined,
        rescueOrdersProjectId: undefined,
        rescueOrdersType: undefined,
        rescueOrdersContent: undefined,
        rescueOrdersPath: undefined,
        rescueOrdersLongitude: undefined,
        rescueOrdersLatitude: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rescueOrdersId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加救援订单 ";
    },
	/** 发货按钮操作 */
	handleUpdatee(row) {
		console.log(row);
	  this.reset();
	  const rescueOrdersId = row.rescueOrdersId || this.ids
	  getOrders(rescueOrdersId).then(response => {
	    this.form = response.data;
	    this.openss = true;
	    this.title = "发货订单";
	  });
	},
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rescueOrdersId = row.rescueOrdersId || this.ids
      getOrders(rescueOrdersId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改救援订单 ";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rescueOrdersId != undefined) {
            updateOrders(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOrders(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
	/** 提交按钮 */
	submitFormm: function() {
	  this.$refs["form"].validate(valid => {
	    if (valid) {
	      if (this.form.rescueOrdersId != undefined) {
			this.form.rescueOrdersType = 2;
	        updateOrders(this.form).then(response => {
	          if (response.code === 200) {
	            this.msgSuccess("修改成功");
	            this.openss = false;
	            this.getList();
	          }
	        });
	      } else {
	        addOrders(this.form).then(response => {
	          if (response.code === 200) {
	            this.msgSuccess("新增成功");
	            this.openss = false;
	            this.getList();
	          }
	        });
	      }
	    }
	  });
	},
	
    /** 删除按钮操作 */
    handleDelete(row) {
      const rescueOrdersIds = row.rescueOrdersId || this.ids;
      this.$confirm('是否确认删除救援订单 编号为"' + rescueOrdersIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrders(rescueOrdersIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有救援订单 数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrders(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>